const vscode = require('vscode');
const hiSmartLiteEx = require('../hiSmartLiteEx');
const authorizationKey = 'giteeAuthorization';

class AccountClass {
  constructor() {
    this.accountData = {
      Authorization: '', //gitee信息base64
      accessToken: '', // gitee access token
      giteeLogin: '', // gitee 邮箱
      giteeUserInfo: {} // gitee登录用户信息
    };
  }

  // 更新globalState信息
  setAuthValue(authDict) {
    const context = hiSmartLiteEx.context;
    const defaultInfo = JSON.stringify(this.accountData);
    const accountStr = context.globalState.get(authorizationKey, defaultInfo);
    const accountData = JSON.parse(accountStr);
    Object.keys(authDict).forEach(key => {
      accountData[key] = authDict[key];
    });
    const str = JSON.stringify(accountData);
    return context.globalState.update(authorizationKey, str);
  }

  // 获取邮箱信息base64
  getAuthorization(account) {
    if (account === '' || account === undefined) {
      return ''
    }
    const context = hiSmartLiteEx.context;
    const accountStr = context.globalState.get(authorizationKey, '{}');
    const accountData = JSON.parse(accountStr);
    const authorization = accountData.Authorization;
    return authorization;
  }

  // 用户登录信息生成base64
  generateAuthorization(account, passwd) {
    let Authorization = '';
    if (account && passwd) {
      Authorization = this.base64Encode(`${account}:${passwd}`);
    }
    return Authorization;
  }

  base64Encode(str) {
    return Buffer.from(str).toString('base64');
  }

  setGiteeUserInfo(obj) {
    return this.setAuthValue({ giteeUserInfo: obj });
  }

  // 获取用户登录所有信息
  getGiteeLoginInfo() {
    const context = hiSmartLiteEx.context;
    const accountStr = context.globalState.get(authorizationKey, '{}');
    const accountInfo = JSON.parse(accountStr);
    return accountInfo;
  }

  // 清空登录信息
  clearAccountData(message = '') {
    this.accountData = {
      Authorization: '',
      accessToken: '',
      giteeLogin: '',
      giteeUserInfo: {}
    };
    const str = JSON.stringify(this.accountData);
    const context = hiSmartLiteEx.context;
    context.globalState.update(authorizationKey, str);
    if (message) {
      vscode.window.showWarningMessage(message);
    }
  }

  redirectLoginInfo(message) {
    if (message) {
      vscode.window.showInformationMessage(message);
      const messageChoose = hiSmartLiteEx.language === 'en' ? 'Please authenticate your login information in the Help Center first' : '请先在帮助中心进行登录信息认证';
      const clickLogin = hiSmartLiteEx.language === 'en' ? 'Click Login' : '点击登录';
      vscode.window.showInformationMessage(messageChoose, clickLogin).then(result => {
        if (result === clickLogin) {
          vscode.commands.executeCommand('HiSmartLiteFoundation.openLoginWebview');
        }
      });
    }
  }

  async isLoginPlug() {
    const loginInfo = this.getGiteeLoginInfo();
    return !!loginInfo.giteeLogin;
  }

  async getAccessToken() {
    const context = hiSmartLiteEx.context;
    const accountStr = context.globalState.get(authorizationKey, '{}');
    const accountData = JSON.parse(accountStr);
    return accountData['accessToken'] || '';
  }

  setAccessToken(username, password, token) {
    const authorization = (!username || !password) ? '' : this.generateAuthorization(username, password);
    return this.setAuthValue({ Authorization: authorization, giteeLogin: username, accessToken: token });
  }
}
module.exports = AccountClass;